<?php
use \Swoole\Table;
$ws = new swoole_websocket_server("0.0.0.0", 9501);
$ws->set([
    'worker_num'    =>4,
    'max_conn'	    =>1000,
    'task_worker_num'    =>200,
    'daemonize'    =>1,
    'log_file'   => '/data/log/swoole.log',
    'user'	=>'root',
    'pipe_buffer_size' => 2 * 1024 *1024,
    'buffer_output_size' => 2 * 1024 *1024,
    'socket_buffer_size' => 2 * 1024 *1024

]);

function sendTo($data){
    global $ws,$table;
    $tofd=(int)$data['tofd'];
    $fd=(int)$data['fd'];
    $type=(int)$data['type'];
    $data=json_encode($data);
    if($tofd==-1){//群发
	foreach($table as $key =>  $val){
	    $ws->push($key,$data);
	}
    }else{
	$ws->push($tofd,$data);
	if($type!=2&&$type!=1){
	    $ws->push($fd,$data);
        }
    }
}

$table = new \Swoole\Table(8);
$table->column('nickname',Table::TYPE_STRING,20);
$table->column('avatar',Table::TYPE_STRING,40);
$table->create();

//监听WebSocket连接打开事件
$ws->on('open', function ($ws, $request) {
    $info=$ws->getClientInfo($request->fd);
    global $table;
    echo "{$info['remote_ip']}[{$info['remote_port']}] connect...\n";
});

//监听WebSocket消息事件
$ws->on('message', function ($ws, $frame) {
    $info=$ws->getClientInfo($frame->fd);
    global $table;
    $data=json_decode($frame->data,true);
    $data['fd']=$frame->fd;
    $data=array_merge($data,$info);
    $list=[];
    switch ((int)$data['type']){
	case 1://新连接
 	     
	case 2://连接建立 
	     $res=$data;
	     $res['type']=1;
    	     $res['tofd']=-1;
    	     sendTo($res);

    	     $list=[];
	     $res['tofd']=$frame->fd;
	     $res['type']=2;
    	     foreach($table as $key =>$val){
        	$list[$key]=$val;
    	     }
    	     $res['list']=$list;
    	     sendTo($res);

	    $table->set($frame->fd,['nickname'   =>$data['userInfo']['nickname'],                   'avatar'   =>$data['userInfo']['avatar']]);
	    break;
	case 4://消息
	case 6://视频数据
	case 7://音频数据
	    $res=$data;
	    $res['userInfo']=$table->get($frame->fd);
	    sendTo($res);
	    break;
	case 5://文件
	    $dir='/var/www/swoole/swoole_chat/upload';
	    $name=$data['file']['name'];
	    $data['userInfo']=$table->get($frame->fd);
	    if(!is_dir($dir)){
		if(!mkdir($dir,0711)){
		    echo $dir."create dir fail...\n";
		}
	    }
	    $fd=fopen($dir.'/'.$name,'a+');
	    if(!$fd){
		echo $dir."/".$name." unable to open...\n";
	    }
	    $base64=preg_replace('/data:.*;base64,/i','',$data['file']['data']);
	    $base64=base64_decode($base64);
	    fwrite($fd,$base64);
	    if($data['file']['sendSize']>=$data['file']['size']){
		$data['file']['url']="/swoole/swoole_chat/upload/".$data['file']['name'];
		sendTo($data);
	    }
	    break;
	default:
    }
});

//监听WebSocket连接关闭事件
$ws->on('close', function ($ws, $fd) {
    $info=$ws->getClientInfo($fd);
    global $table;
    $table->del($fd);
    $info['type']=3;
    $info['fd']=$fd;
    $info['tofd']=-1;
    $info['userInfo']=$table->get($fd);
    sendTo($info);
    echo "{$info['remote_ip']}[{$info['remote_port']}] close\n";
});

$ws->on('shutdown',function(){
    echo "Server 正常结束\n";
    unlink("/data/log/swoole.log");
	$res=system("rm -rf /var/www/swoole/swoole_chat/upload");
	echo $res;
});

$ws->on('task',function($ws,$task_id,$worker_id,$data){

});

$ws->on('finish',function($ws,$task_id,$data){

});

$ws->start();

?>
